/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.util.log;

import java.util.Date;

final class LoggerImpl implements Logger {

	static String getDateTime() {
		Date date = new Date(System.currentTimeMillis());
		return date.toString();
	}

	private Appender appender;

	private Class className;

	public final void error(String message) {

		StringBuffer buf = new StringBuffer('[' + getDateTime()).append(']').append(" ").append(ERROR).append(" ")
				.append('[').append(className.getName()).append(']').append(" ");
		buf.append(message);
		appender.error(buf);
	}

	public final void error(String message, Throwable e) {
		printMsgError(message, ERROR, e);
	}

	public final void fatal(String message, Throwable e) {
		printMsgError(message, FATAL, e);
	}

	public void info(String message) {

		StringBuffer buf = new StringBuffer('[' + getDateTime()).append(']').append(" ").append(INFO).append(" ")
				.append('[').append(className.getName()).append(']').append(" ");
		buf.append(message);
		appender.error(buf);

	}

	private void printMsgError(String message, String type, Throwable e) {
		StringBuffer buf = new StringBuffer('[' + getDateTime()).append(']').append(" ").append(type).append(" ")
				.append('[').append(className.getName()).append(']').append(" ");
		buf.append(message).append("  " + e.toString());
		e.printStackTrace();
	}

	final void setAppender(Appender appender) {
		this.appender = appender;
	}

	final void setClass(Class name) {
		this.className = name;
	}

	public final void warn(String message) {
		StringBuffer buf = new StringBuffer('[' + getDateTime()).append(']').append(" ").append(WARN).append(" ")
				.append('[').append(className.getName()).append(']').append(" ");
		buf.append(message);
		appender.error(buf);
	}

}
